﻿using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Utils
{
    public class Config
    {
        //配置文件读取
        /// <summary>
        /// 修改INI配置文件
        /// </summary>
        /// <param name="section">段落</param>
        /// <param name="key">关键字</param>
        /// <param name="val">新值</param>
        /// <param name="filepath">文件完整路径</param>
        /// <returns>void</returns>
        [DllImport("kernel32")]
        public static extern long WritePrivateProfileString(string section, string key, string val, string filepath);

        /// <summary>
        /// 读INI配置文件
        /// </summary>
        /// <param name="section">段落</param>
        /// <param name="key">关键字</param>
        /// <param name="def">缺省值</param>
        /// <param name="retval">读取至</param>
        /// <param name="size">指定装载到lpReturnedString缓冲区的最大字符数量</param>
        /// <param name="filePath">文件完整路径</param>
        /// <returns>void</returns>
        [DllImport("kernel32")]
        static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);

        /// <summary>
        /// 读INI配置文件
        /// </summary>
        /// <param name="section">段落</param>
        /// <param name="key">关键字</param>
        /// <param name="def">缺省值</param>
        /// <param name="filePath">文件完整路径</param>
        /// <returns>string</returns>
        public static string GetPrivateProfileString(string section, string key, string def, string filePath)
        {
            StringBuilder strbdr = new StringBuilder();
            GetPrivateProfileString(section, key, def, strbdr, 255, filePath);
            return strbdr.ToString();
        }
    }
}
